#include "RobotController.h"
#include "MainWindow.h"
#include "Ebn.h"
#include "EbnMonitor.h"

#include <QString>

#define XML_FILE "model.xml"

CRobotController::CRobotController(MainWindow * pWindow)
    : CEbnModelController(0)
{
    mpMainWindow = pWindow;
    mpMonitor = new CEbnMonitor(mpModel);
}

CRobotController::~CRobotController()
{
    if (mpMonitor)
    {
        delete mpMonitor;
        mpMonitor = NULL;
    }

    mpMainWindow = NULL;
}

void CRobotController::initializeModel(void)
{
    qDebug("init Model\n");
    loadModelFile(XML_FILE);
}

void CRobotController::uninitializeModel(void)
{
    if (mpModel)
    {
        mpModel->clear();
    }


    if (mpWorldStateManager)
    {
        mpWorldStateManager->clear();
    }

    if (mpActionManager)
    {
        mpActionManager->clear();
    }

}

void CRobotController::onMsgReceived(string msgName)
{
    CEbnAction * action = mpActionManager->getAction(msgName);

    if (action)
    {

        QString filename(QString::fromLocal8Bit(action->getAnimation().c_str()));
        qDebug() << "play action :" << filename;
        mpMainWindow->playAction(filename);

    }
}

void CRobotController::startController(void)
{
    QString msg("running....");
    if (mpMonitor)
    {
        mpMonitor->appendMsgOutput(msg);
    }
    run();
}

void CRobotController::stopController(void)
{
    QString msg("stopping....");
    if (mpMonitor)
    {
        mpMonitor->appendMsgOutput(msg);
    }
    stop();
}

void CRobotController::showMonitor(void)
{
    if (mpMonitor)
    {
        mpMonitor->show();
    }
}

void CRobotController::hideMonitor(void)
{
    if (mpMonitor)
    {
        mpMonitor->hide();
    }
}

void CRobotController::sayHello(void)
{
    QString msg("Heard hello....");
    if (mpMonitor)
    {
        mpMonitor->appendMsgOutput(msg);
    }
    if (mpWorldStateManager)
    {
        mpWorldStateManager->setWorldStateValue("Heard-Hello", 1);
    }
}

void CRobotController::smile(void)
{
    QString msg("See laughing....");
    if (mpMonitor)
    {
        mpMonitor->appendMsgOutput(msg);
    }
    if (mpWorldStateManager)
    {
        mpWorldStateManager->setWorldStateValue("See-Laugh", 1);
    }
}

void CRobotController::awkward(void)
{
    QString msg("See awkward....");
    if (mpMonitor)
    {
        mpMonitor->appendMsgOutput(msg);
    }
    if (mpWorldStateManager)
    {
        mpWorldStateManager->setWorldStateValue("See-Awkward", 1);
    }
}

void CRobotController::encourage(void)
{
    QString msg("want a hug?....");
    if (mpMonitor)
    {
        mpMonitor->appendMsgOutput(msg);
    }
    if (mpWorldStateManager)
    {
        mpWorldStateManager->setWorldStateValue("Child-Respond", 1);
    }
}
